<template>
  <div id="setId">
			<div class="icon-style col-fff text-cent font-46">
				<img src="https://app.zbzedu.com/static/WaChat/img/set_pw.png" />
			 </div>
			<div class="font-28 col-333 text-cent">请{{showText}}登录密码</div>
			<div class="font-28 col-333 text-cent pd_b_45">后期也可通过手机号+密码方式登录</div>
			<div class="pd_l_30 pd_r_30 pd_t_60">
				<div v-if="!show" class="font-26 yh_bd_bottom pd_t_30 pd_b_20">
					<input type="password" class="col-333" v-model="oldPassword" value="" placeholder="请输入旧密码" />
				</div>
				<div class="font-26 yh_bd_bottom pd_t_30 pd_b_20">
					<input type="password" v-model="basePassword" class="col-333" value="" placeholder="请输入密码" />
				</div>
				<div class="font-26 yh_bd_bottom pd_t_30 pd_b_20">
					<input type="password" class="col-333" v-model="basePassword1" value="" placeholder="请再次输入密码" />
				</div>
			</div>
			<div class="user-btn text-cent col-fff font-30 bg-1f476d" @click="login">确认</div>
		</div>
</template>

<script>
	import {TOAST} from "../../util/util";
	import {request} from "../../util/api";

	export default {
		data() {
			return {
				show: false,
				showText: '修改',
				password: '',
				basePassword: '',
				oldPassword: '',
				basePassword1: '',
				num: 0
			}
		},

		onLoad(option) {
			this.num = option.num
			if (option.num == 2) {
				this.show = true;
				uni.setNavigationBarTitle({
						title: '设置密码'
				});
				this.showText = '设置';
			} else{
				this.show = false;
				this.showText = '修改';
				uni.setNavigationBarTitle({
						title: '修改密码'
				});
			}
			if (option.password){
				this.password = option.password;
			}
		},

		methods: {
			openWindow: function (path) {
				//页面跳转，xiaoyao
				$App.openWin({
					name: path,
					url: 'widget://' + path + '.html',
				})

			},
			login() {
				var that_ = this;
				if (this.num != 2) {
					if (that_.password != that_.oldPassword) {
						TOAST('请输入正确的旧密码');
						return false;
					}
					if (that_.password == that_.basePassword) {
						TOAST('不能与旧密码相同');
						return false;
					}
				}
				if (!this.checkPassword(this.basePassword)) {
					return false;
				}
				if (this.basePassword != this.basePassword1) {
					TOAST('两次密码不相同');
					return false;
				}

				request({
					url: '/app/base/setPassword',
					data: {
						"oldPassword": this.oldPassword,
						"password": this.basePassword,
						"repassword": this.basePassword1,
					},
					method: 'post'
				}).then(ret => {
					uni.showModal({
						title: '提示',
						content: '修改密码成功',
						showCancel: false,
						success: function (res) {
							uni.navigateBack()
						}
					})
				})
			},

			checkPassword(password) {
				if (!password || password.length < 6) {
					TOAST('密码至少6位');
					return false;
				}
				var passwordReg = /[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/;
				if (passwordReg.test(password)) {
					return true;
				} else {
					TOAST('密码必须包含数字、字母')
					return false;
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user-btn {
				width: 90%;
				height: 88upx;
				border-radius: 44upx;
				line-height: 88upx;
				margin: 150upx auto 0;
			}

			.icon-style{
				width: 160upx;
				height: 160upx;
				margin: 70upx auto 0;
			}

			.icon-style img{
				width: 100%;
				height: 100%;
			}

			.user-btn {
				width: 90%;
				height: 88upx;
				border-radius: 44upx;
				line-height: 88upx;
				margin: 260upx auto 50upx;
			}

			.skip-btn {
				width: 90%;
				height: 88upx;
				border-radius:44upx;
				line-height: 88upx;
				border: 1upx solid #CCCCCC;
				margin: 0 auto;
			}
	.bg-1f476d {
		background: #1f476d;
	}
</style>